/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.msg;

import cn.ac.iie.di.ban.ycl.idgen.IDGeneratorInterface;
import cn.ac.iie.di.ban.ycl.idgen.IDSHAGeneratorImpl;
import cn.ac.iie.di.datadock.rdata.exchange.client.v1.session.RESendSession;
import cn.ac.iie.di.gloabl.GlobalParas;
import cn.ac.iie.di.msg.JsonMsgSender.SessionPool;
import cn.ac.iie.di.tools.GenerateMD5;
import cn.ac.iie.di.tools.RemoveDuplicate;

//import cn.ac.iie.di.tools.LongHashCode;
//import cn.ac.iie.di.tools.MMUrlPath;
import cn.ac.iie.di.tools.SplitJsonArray;
import cn.ac.iie.di.tools.TimeFormat;
import static com.trs.ckm.samples.SysInterfaceTest.client;
import com.trs.ckm.soap.ABSHold;
import com.trs.ckm.soap.ABSHold;
import com.trs.ckm.soap.ABSResult;
import com.trs.ckm.soap.CkmSoapException;
import com.trs.ckm.soap.PLOResult;
import com.trs.ckm.soap.RuleCATField;
import com.trs.ckm.soap.TrsCkmSoapClient;
import com.trs.ckm.soap.TrsCkmSoapClient;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
//import cn.ac.iie.di.tools.TimeTransfer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import javax.crypto.Mac;
import org.apache.log4j.Logger;

/**
 *
 * @author lenovo-pc
 */
public class JsonMsgTask1 implements Runnable {

    private final List<String> jsonList;

    private HashMap<String, String> JsonData = new HashMap<>();//存储Json中的数据1
    static private final Logger logger = Logger.getLogger(JsonMsgTask1.class.getName());
    static private final AtomicInteger count = new AtomicInteger(0);

    public static void printLog(int n) {
        if (count.addAndGet(n) > 500) {
            boolean print = false;
            int cur = 0;
            synchronized (count) {
                cur = count.get();
                if (cur > 500) {
                    count.addAndGet(-cur);
                    print = true;
                }
            }
            if (print) {
                logger.info("The imsg parser " + cur);
            }
        }
    }

    private final SessionPool pool;

    public JsonMsgTask1(List<String> jsonList, SessionPool pool) {
        this.jsonList = jsonList;
        this.pool = pool;

    }

    @Override
    public void run() {
        RESendSession session = null;
        SplitJsonArray gmsgJson = new SplitJsonArray();
        String str_pid = "";
        long line = 0;
        try {
            session = pool.borrowSession();
            for (String json : jsonList) {

                if (json != null && !json.isEmpty()) {

//                    String[] str = new String[];
                    session.setString("wx_lfile_name", GlobalParas.DES_MSG);
                    String str = "0|" + json + "|" + GlobalParas.FTP_IP;
                    session.setStrings("ifexists", new String[]{str});
                    try {

                        try {
                            session.add();
                            line++;
                        } catch (Exception e) {
                            sendMSG(session);
                            logger.info("MSG successful send " + line);
                            line = 0;
                            session.add();
                            line++;
                        }

                    } catch (Exception ex) {
                        logger.error("The session add is wrong:" + ex, ex);
                    }
                }
            }
            sendMSG(session);
            logger.info("MSG successful send " + line);
            line = 0;

        } catch (Exception ex) {
            logger.error("know group Task error " + ex, ex);
        } finally {
            if (session != null) {
                pool.returnSession(session);
            }
        }
    }

    public void sendMSG(RESendSession session) {
        while (true) {
            try {
                session.flush();
                break;
            } catch (Exception ex) {
                logger.info("what's up buddy ? how's it going ?" + ex, ex);
            }
        }
    }

    public static String getException(Exception e) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        PrintStream pout = new PrintStream(out);
        e.printStackTrace(pout);
        String ret = new String(out.toByteArray());
        pout.close();
        try {
            out.close();
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(JsonMsgSender.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ret;
    }

}
